package com.liuzy.crawler;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.wb.swt.SWTResourceManager;

import com.liuzy.crawler.core.Crawler;
import com.liuzy.crawler.core.FileScan;
import com.liuzy.crawler.util.StringUtil;

/**
 * @author liuzy
 * @since 2015年9月21日
 */
public class Main {
	private static Display display;
	private static Shell shell;
	private static MenuItem menuF, fileOpen, fileExport, exit;
	private static MenuItem menuV, ckCache, ckScan, ckDl;
	private static MenuItem menuS, setOutput, setCache, setScan, setDl, setAll;
	private static MenuItem menuH, update, help, reply, pay, about;
	private static List list;
	private static Text txtUrl, txtMsg;
	private static Button btnAdd, btnClear, btnStart;
	private static Label lCache_1, lCache_2, lCache_3, lCache_4, lCache_5, lCache_6, lCache_7, lCache_8;
	private static Label lScan_1, lScan_2, lScan_3, lScan_4, lScan_5, lScan_6, lScan_7, lScan_8;
	private static Label ldl_1, ldl_2, ldl_3, ldl_4, ldl_5, ldl_6, ldl_7, ldl_8;
	private static Timer timer;
	private static boolean menuExit;

	public static void main(String[] args) throws Exception {
		display = Display.getDefault();

		shell = new Shell(display, SWT.CLOSE | SWT.MIN);
		shell.setModified(true);
		shell.setText("Crawler 1.0");
		shell.setSize(800, 600);
		shell.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		shell.setLocation(display.getClientArea().width / 2 - shell.getShell().getSize().x / 2, display.getClientArea().height / 2 - shell.getSize().y / 2);
		shell.setLayout(new FormLayout());
		shell.setImage(SWTResourceManager.getImage(Main.class, "logo.jpg"));

		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);

		menuF = new MenuItem(menu, SWT.CASCADE);
		menuF.setText("文件(&F)");

		Menu mmFile = new Menu(menuF);
		menuF.setMenu(mmFile);

		fileOpen = new MenuItem(mmFile, SWT.NONE);
		fileOpen.setText("打开文件(&O)\tALT+O");
		fileOpen.setAccelerator(SWT.ALT + 'O');
		fileOpen.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				FileDialog fileOpen = new FileDialog(shell, SWT.OPEN);
				fileOpen.setFilterNames(new String[]{"网页文件(*.html;*.htm)", "文本文件(*.txt)", "所有文件(*.*)"});
				fileOpen.setFilterExtensions(new String[]{"*.html;*.htm", "*.txt", "*.*"});
				String path = fileOpen.open();
				if (path != null) {
					try {
						FileScan fileScan = new FileScan(path);
						for (String url : fileScan.scanCache()) {
							list.add(url);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});

		new MenuItem(mmFile, SWT.SEPARATOR);

		fileExport = new MenuItem(mmFile, SWT.NONE);
		fileExport.setText("导出日志(&E)\tALT+E");
		fileExport.setAccelerator(SWT.ALT + 'E');
		fileExport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fileSave = new FileDialog(shell, SWT.SAVE);
				String path = fileSave.open();
				if (path != null) {
					Path file = FileSystems.getDefault().getPath(path);
					try {
						Files.write(file, txtMsg.getText().getBytes());
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}
		});

		new MenuItem(mmFile, SWT.SEPARATOR);

		exit = new MenuItem(mmFile, SWT.NONE);
		exit.setText("退出(&X)");
		exit.setAccelerator(SWT.ALT + SWT.F4);
		exit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				menuExit = true;
				new Thread() {
					private int width = shell.getSize().x;
					private int hight = shell.getSize().y;
					public void run() {
						while (hight > 0) {
							shell.getDisplay().syncExec(new Runnable() {
								public void run() {
									shell.setSize(width, hight -= 3);
								}
							});
						}
						while (width > 0) {
							shell.getDisplay().syncExec(new Runnable() {
								public void run() {
									shell.setSize(width -= 5, hight);
								}
							});
						}
						shell.getDisplay().syncExec(new Runnable() {
							public void run() {
								shell.close();
							}
						});
					}
				}.start();
			}
		});

		menuV = new MenuItem(menu, SWT.CASCADE);
		menuV.setText("查看(&V)");

		Menu mmView = new Menu(menuV);
		menuV.setMenu(mmView);

		ckCache = new MenuItem(mmView, SWT.CHECK);
		ckCache.setSelection(true);
		ckCache.setText("缓存文件日志");
		ckCache.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Config.cacheLog = ckCache.getSelection();
			}
		});
		ckScan = new MenuItem(mmView, SWT.CHECK);
		ckScan.setSelection(true);
		ckScan.setText("扫描文件日志");
		ckScan.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Config.scanLog = ckScan.getSelection();
			}
		});
		ckDl = new MenuItem(mmView, SWT.CHECK);
		ckDl.setSelection(true);
		ckDl.setText("下载图片日志");
		ckDl.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Config.dlLog = ckDl.getSelection();
			}
		});

		menuS = new MenuItem(menu, SWT.CASCADE);
		menuS.setText("设置(&S)");

		Menu mmSet = new Menu(menuS);
		menuS.setMenu(mmSet);

		setOutput = new MenuItem(mmSet, SWT.NONE);
		setOutput.setText("下载保存目录(&O)");
		setOutput.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog openDir = new DirectoryDialog(shell, SWT.OPEN);
				openDir.setText("图片保存到...");
				String dir = openDir.open();
				if (dir != null) {
					Config.dlDir = dir + File.separator;
				}
			}
		});

		new MenuItem(mmSet, SWT.SEPARATOR);

		setCache = new MenuItem(mmSet, SWT.NONE);
		setCache.setText("缓存池(&C)");
		setCache.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Setting.show(shell, 'C');
			}
		});

		setScan = new MenuItem(mmSet, SWT.NONE);
		setScan.setText("扫描池(&S)");
		setScan.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Setting.show(shell, 'S');
			}
		});

		setDl = new MenuItem(mmSet, SWT.NONE);
		setDl.setText("下载池(&D)");
		setDl.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Setting.show(shell, 'D');
			}
		});

		new MenuItem(mmSet, SWT.SEPARATOR);

		setAll = new MenuItem(mmSet, SWT.NONE);
		setAll.setText("选项(&B)\tALT+B");
		setAll.setAccelerator(SWT.ALT + 'B');
		setAll.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Setting.show(shell, 'B');
			}
		});

		menuH = new MenuItem(menu, SWT.CASCADE);
		menuH.setText("帮助(&H)");

		Menu mmHelp = new Menu(menuH);
		menuH.setMenu(mmHelp);

		update = new MenuItem(mmHelp, SWT.NONE);
		update.setText("检查更新(&U)");
		update.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});

		new MenuItem(mmHelp, SWT.SEPARATOR);

		help = new MenuItem(mmHelp, SWT.NONE);
		help.setText("获得帮助(&G)");
		help.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://liuzhongyang.com/Crawler/help");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		reply = new MenuItem(mmHelp, SWT.NONE);
		reply.setText("问题反馈(&Q)");
		reply.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://liuzhongyang.com/Crawler/suggestion");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		pay = new MenuItem(mmHelp, SWT.NONE);
		pay.setText("捐助(&P)");
		pay.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Donate.show(shell);
			}
		});

		new MenuItem(mmHelp, SWT.SEPARATOR);

		about = new MenuItem(mmHelp, SWT.NONE);
		about.setText("关于(&A)");
		about.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				About.show(shell);
			}
		});

		FormToolkit formToolkit = new FormToolkit(display);

		btnAdd = new Button(shell, SWT.NONE);
		btnAdd.setToolTipText("添加网址");
		btnAdd.setCursor(new Cursor(display, SWT.CURSOR_HAND));
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String url = txtUrl.getText().replaceFirst("http://", "");
				if (StringUtil.isUrl(url)) {
					list.add(url);
					txtUrl.setText("");
					txtUrl.forceFocus();
				} else {
					txtUrl.forceFocus();
					txtUrl.selectAll();
				}
			}
		});
		FormData fd_btnNewButton = new FormData();
		btnAdd.setLayoutData(fd_btnNewButton);
		formToolkit.adapt(btnAdd, true, true);
		btnAdd.setText("添加");

		btnClear = new Button(shell, SWT.NONE);
		btnClear.setToolTipText("清空全部列表或清除选中项");
		btnClear.setCursor(new Cursor(display, SWT.CURSOR_HAND));
		btnClear.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (list.getSelectionCount() > 0) {
					list.remove(list.getSelectionIndices());
				} else {
					list.removeAll();
					txtUrl.forceFocus();
				}
			}
		});
		FormData fd_btnNewButton_1 = new FormData();
		fd_btnNewButton_1.left = new FormAttachment(btnAdd, 6);
		btnClear.setLayoutData(fd_btnNewButton_1);
		formToolkit.adapt(btnClear, true, true);
		btnClear.setText("清空");

		txtUrl = new Text(shell, SWT.BORDER);
		txtUrl.setToolTipText("输入你要爬的网址哦");
		fd_btnNewButton.right = new FormAttachment(txtUrl, 42, SWT.RIGHT);
		fd_btnNewButton.top = new FormAttachment(txtUrl, -2, SWT.TOP);
		fd_btnNewButton.left = new FormAttachment(txtUrl, 6);
		txtUrl.setText("www.mm131.com");
		FormData fd_url = new FormData();
		fd_url.right = new FormAttachment(0, 269);
		fd_url.left = new FormAttachment(0, 10);
		txtUrl.setLayoutData(fd_url);
		formToolkit.adapt(txtUrl, true, true);

		list = new List(shell, SWT.BORDER | SWT.V_SCROLL);
		list.setToolTipText("网址列表");
		fd_url.top = new FormAttachment(list, 6);
		fd_btnNewButton_1.top = new FormAttachment(list, 4);
		list.setItems(new String[]{"www.mm8mm8.com", "www.zngirls.com"});
		FormData fd_list = new FormData();
		fd_list.bottom = new FormAttachment(0, 214);
		fd_list.right = new FormAttachment(0, 395);
		fd_list.top = new FormAttachment(0, 10);
		fd_list.left = new FormAttachment(0, 10);
		list.setLayoutData(fd_list);
		formToolkit.adapt(list, true, true);

		btnStart = new Button(shell, SWT.NONE);
		btnStart.setToolTipText("开始爬呀爬...");
		btnStart.setCursor(new Cursor(display, SWT.CURSOR_HAND));
		fd_btnNewButton_1.right = new FormAttachment(btnStart, -6);
		btnStart.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (!Crawler.isRun()) {
					if (list.getItemCount() > 0) {
						Crawler.startup(list.getItems());
						btnAdd.setEnabled(false);
						btnClear.setEnabled(false);
						btnStart.setText("停止");
						timer = new Timer();
						refresh();
					} else {
						txtUrl.forceFocus();
						txtUrl.selectAll();
					}
				} else {
					btnStart.setText("稍等");
					btnStart.setEnabled(false);
					if (Crawler.shutdown()) {
						timer.cancel();
						btnStart.setText("开始");
						btnStart.setEnabled(true);
						btnAdd.setEnabled(true);
						btnClear.setEnabled(true);
					}
				}
			}
		});
		FormData fd_button = new FormData();
		fd_button.top = new FormAttachment(list, 4);
		fd_button.right = new FormAttachment(0, 395);
		fd_button.left = new FormAttachment(0, 359);
		btnStart.setLayoutData(fd_button);
		formToolkit.adapt(btnStart, true, true);
		btnStart.setText("开始");

		Group gpCache = new Group(shell, SWT.NONE);
		gpCache.setText("缓存池");
		FormData fd_group_1 = new FormData();
		fd_group_1.right = new FormAttachment(100, -10);
		fd_group_1.left = new FormAttachment(100, -383);
		fd_group_1.top = new FormAttachment(0, 7);
		fd_group_1.bottom = new FormAttachment(0, 197);
		gpCache.setLayoutData(fd_group_1);
		formToolkit.adapt(gpCache);
		formToolkit.paintBordersFor(gpCache);

		Group gpScan = new Group(shell, SWT.NONE);
		gpScan.setText("扫描池");
		FormData fd_group_2 = new FormData();
		fd_group_2.top = new FormAttachment(gpCache, 7);
		fd_group_2.right = new FormAttachment(100, -10);
		fd_group_2.left = new FormAttachment(100, -383);

		lCache_1 = new Label(gpCache, SWT.NONE);
		lCache_1.setBounds(51, 26, 135, 17);
		formToolkit.adapt(lCache_1, true, true);
		lCache_1.setText("添加缓存");

		lCache_2 = new Label(gpCache, SWT.NONE);
		lCache_2.setToolTipText("添加地址到队列历史");
		lCache_2.setText("添加成功");
		lCache_2.setBounds(51, 67, 135, 17);
		formToolkit.adapt(lCache_2, true, true);

		lCache_3 = new Label(gpCache, SWT.NONE);
		lCache_3.setToolTipText("工作线程从队列中取出");
		lCache_3.setText("取出次数");
		lCache_3.setBounds(51, 108, 135, 17);
		formToolkit.adapt(lCache_3, true, true);

		lCache_4 = new Label(gpCache, SWT.NONE);
		lCache_4.setText("队列剩余");
		lCache_4.setBounds(51, 149, 135, 17);
		formToolkit.adapt(lCache_4, true, true);

		lCache_5 = new Label(gpCache, SWT.NONE);
		lCache_5.setToolTipText("当前缓存池工作线程数量");
		lCache_5.setText("缓存线程");
		lCache_5.setBounds(208, 26, 135, 17);
		formToolkit.adapt(lCache_5, true, true);

		lCache_6 = new Label(gpCache, SWT.NONE);
		lCache_6.setToolTipText("目标文件已存在");
		lCache_6.setText("缓存取消");
		lCache_6.setBounds(208, 67, 135, 17);
		formToolkit.adapt(lCache_6, true, true);

		lCache_7 = new Label(gpCache, SWT.NONE);
		lCache_7.setToolTipText("下载页面超时");
		lCache_7.setText("缓存失败");
		lCache_7.setBounds(208, 108, 135, 17);
		formToolkit.adapt(lCache_7, true, true);

		lCache_8 = new Label(gpCache, SWT.NONE);
		lCache_8.setText("缓存成功");
		lCache_8.setBounds(208, 149, 135, 17);
		formToolkit.adapt(lCache_8, true, true);
		gpScan.setLayoutData(fd_group_2);
		formToolkit.adapt(gpScan);
		formToolkit.paintBordersFor(gpScan);

		Group gpDl = new Group(shell, SWT.NONE);
		fd_group_2.bottom = new FormAttachment(gpDl, -7);
		gpDl.setText("下载池");
		FormData fd_group_3 = new FormData();
		fd_group_3.right = new FormAttachment(100, -10);
		fd_group_3.left = new FormAttachment(100, -383);
		fd_group_3.bottom = new FormAttachment(100, -8);
		fd_group_3.top = new FormAttachment(0, 383);

		lScan_1 = new Label(gpScan, SWT.NONE);
		lScan_1.setText("添加文件");
		lScan_1.setBounds(50, 21, 135, 17);
		formToolkit.adapt(lScan_1, true, true);

		lScan_2 = new Label(gpScan, SWT.NONE);
		lScan_2.setToolTipText("添加路径到队列历史");
		lScan_2.setText("添加成功");
		lScan_2.setBounds(50, 58, 135, 17);
		formToolkit.adapt(lScan_2, true, true);

		lScan_3 = new Label(gpScan, SWT.NONE);
		lScan_3.setToolTipText("工作线程从队列中取出路径");
		lScan_3.setText("取出文件");
		lScan_3.setBounds(50, 95, 135, 17);
		formToolkit.adapt(lScan_3, true, true);

		lScan_4 = new Label(gpScan, SWT.NONE);
		lScan_4.setText("队列剩余");
		lScan_4.setBounds(50, 132, 135, 17);
		formToolkit.adapt(lScan_4, true, true);

		lScan_5 = new Label(gpScan, SWT.NONE);
		lScan_5.setToolTipText("当前扫描池工作线程数量");
		lScan_5.setText("扫描线程");
		lScan_5.setBounds(207, 21, 135, 17);
		formToolkit.adapt(lScan_5, true, true);

		lScan_6 = new Label(gpScan, SWT.NONE);
		lScan_6.setToolTipText("目标文件不存在");
		lScan_6.setText("扫描取消");
		lScan_6.setBounds(207, 58, 135, 17);
		formToolkit.adapt(lScan_6, true, true);

		lScan_7 = new Label(gpScan, SWT.NONE);
		lScan_7.setToolTipText("读文件异常");
		lScan_7.setText("扫描失败");
		lScan_7.setBounds(207, 95, 135, 17);
		formToolkit.adapt(lScan_7, true, true);

		lScan_8 = new Label(gpScan, SWT.NONE);
		lScan_8.setText("扫描完成");
		lScan_8.setBounds(207, 132, 135, 17);
		formToolkit.adapt(lScan_8, true, true);
		gpDl.setLayoutData(fd_group_3);
		formToolkit.adapt(gpDl);
		formToolkit.paintBordersFor(gpDl);

		ldl_1 = new Label(gpDl, SWT.NONE);
		ldl_1.setText("添加下载");
		ldl_1.setBounds(52, 18, 135, 17);
		formToolkit.adapt(ldl_1, true, true);

		ldl_2 = new Label(gpDl, SWT.NONE);
		ldl_2.setToolTipText("添加地址到队列历史");
		ldl_2.setText("添加成功");
		ldl_2.setBounds(52, 53, 135, 17);
		formToolkit.adapt(ldl_2, true, true);

		ldl_3 = new Label(gpDl, SWT.NONE);
		ldl_3.setToolTipText("工作线程从队列中取出");
		ldl_3.setText("取出次数");
		ldl_3.setBounds(52, 88, 135, 17);
		formToolkit.adapt(ldl_3, true, true);

		ldl_4 = new Label(gpDl, SWT.NONE);
		ldl_4.setText("队列剩余");
		ldl_4.setBounds(52, 123, 135, 17);
		formToolkit.adapt(ldl_4, true, true);

		ldl_5 = new Label(gpDl, SWT.NONE);
		ldl_5.setToolTipText("当前下载池工作线程数量");
		ldl_5.setText("下载线程");
		ldl_5.setBounds(208, 18, 135, 17);
		formToolkit.adapt(ldl_5, true, true);

		ldl_6 = new Label(gpDl, SWT.NONE);
		ldl_6.setToolTipText("目标文件已存在");
		ldl_6.setText("下载取消");
		ldl_6.setBounds(208, 53, 135, 17);
		formToolkit.adapt(ldl_6, true, true);

		ldl_7 = new Label(gpDl, SWT.NONE);
		ldl_7.setToolTipText("下载文件超时");
		ldl_7.setText("下载失败");
		ldl_7.setBounds(208, 88, 135, 17);
		formToolkit.adapt(ldl_7, true, true);

		ldl_8 = new Label(gpDl, SWT.NONE);
		ldl_8.setText("下载成功");
		ldl_8.setBounds(208, 123, 135, 17);
		formToolkit.adapt(ldl_8, true, true);

		txtMsg = new Text(shell, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		txtMsg.setToolTipText("日志信息");
		FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(txtUrl, 385);
		fd_text.bottom = new FormAttachment(btnAdd, 297, SWT.BOTTOM);
		fd_text.top = new FormAttachment(btnAdd, 4);
		fd_text.left = new FormAttachment(txtUrl, 0, SWT.LEFT);
		txtMsg.setLayoutData(fd_text);
		formToolkit.adapt(txtMsg, true, true);

		shell.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				if (!menuExit) {
					MessageBox messageBox = new MessageBox(shell, SWT.ICON_WARNING | SWT.OK | SWT.CANCEL);
					messageBox.setText("确定");
					messageBox.setMessage("你确定要退出吗？");
					int rc = messageBox.open();
					if (rc == SWT.CANCEL) {
						e.doit = false;
					} else {
						Crawler.shutdown();
					}
				}
			}
		});

		Config.load();
		ckCache.setSelection(Config.cacheLog);
		ckScan.setSelection(Config.scanLog);
		ckDl.setSelection(Config.dlLog);

		shell.open();

		shell.layout();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	private static void refresh() {
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				display.asyncExec(new Runnable() {
					@Override
					public void run() {
						int[] cache = Crawler.cachePool.getStatus();
						int[] scan = Crawler.scannerPool.getStatus();
						int[] dl = Crawler.downLoadPool.getStatus();
						lCache_1.setText(String.format("添加缓存%10d", cache[0]));
						lCache_2.setText(String.format("添加成功%10d", cache[1]));
						lCache_3.setText(String.format("取出次数%10d", cache[2]));
						lCache_4.setText(String.format("队列剩余%10d", cache[3]));
						lCache_5.setText(String.format("缓存线程%10d", cache[4]));
						lCache_6.setText(String.format("缓存取消%10d", cache[5]));
						lCache_7.setText(String.format("缓存失败%10d", cache[6]));
						lCache_8.setText(String.format("缓存成功%10d", cache[7]));
						lScan_1.setText(String.format("添加文件%10d", scan[0]));
						lScan_2.setText(String.format("添加成功%10d", scan[1]));
						lScan_3.setText(String.format("取出文件%10d", scan[2]));
						lScan_4.setText(String.format("队列剩余%10d", scan[3]));
						lScan_5.setText(String.format("扫描线程%10d", scan[4]));
						lScan_6.setText(String.format("扫描取消%10d", scan[5]));
						lScan_7.setText(String.format("扫描失败%10d", scan[6]));
						lScan_8.setText(String.format("扫描完成%10d", scan[7]));
						ldl_1.setText(String.format("添加下载%10d", dl[0]));
						ldl_2.setText(String.format("添加成功%10d", dl[1]));
						ldl_3.setText(String.format("取出次数%10d", dl[2]));
						ldl_4.setText(String.format("队列剩余%10d", dl[3]));
						ldl_5.setText(String.format("下载线程%10d", dl[4]));
						ldl_6.setText(String.format("下载取消%10d", dl[5]));
						ldl_7.setText(String.format("下载失败%10d", dl[6]));
						ldl_8.setText(String.format("下载成功%10d", dl[7]));
					}
				});
			}
		}, 10, 100);
	}

	public static void showMsg(final String msg) {
		synchronized (txtMsg) {
			display.asyncExec(new Runnable() {
				@Override
				public void run() {
					if (txtMsg.getLineCount() > 1000) {
						txtMsg.setText("");
					}
					txtMsg.append(msg);
				}
			});
		}
	}
}
